package com.lmiot.xyclick.Util;

import android.text.TextUtils;
import com.lmiot.xyclick.Bean.DoAutoBean;
import com.lmiot.xyclick.Bean.SQL.AutoBean;
import com.lmiot.xyclick.Bean.SQL.AutoBeanSqlUtil;
import com.lmiot.xyclick.Bean.TimerBean;
import com.lmiot.xyclick.inteface.OnBasicListener;
import com.youyi.yyviewsdklibrary.ArrayGson;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import org.greenrobot.eventbus.EventBus;

public class AlarmUtil {
    private static final String TAG = "AlarmUtil";
    private static Timer timer;
    private static TimerTask timerTask;
    static String[] typeList01 = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"};
    static String[] typeList02 = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"};

    public static void startTimerAlarm() {
        cancelTimer();
        timer = new Timer();
        TimerTask r2 = new TimerTask() {
            /* class com.lmiot.xyclick.Util.AlarmUtil.AnonymousClass1 */

            public void run() {
                if (PhoneUtil.getAPPVersion().startsWith("TB")) {
                    AlarmUtil.onLineTime();
                } else {
                    AlarmUtil.localTime();
                }
            }
        };
        timerTask = r2;
        timer.schedule(r2, 1000, 1000);
    }

    public static void onLineTime() {
        HttpUtilNew.getInstance().getTime(new OnBasicListener() {
            @Override
            public void result(boolean z, String str) {
                LogUtil.d(AlarmUtil.TAG, "测试淘宝时间：" + z + ":" + str);
                List<AutoBean> searchAllTime = AutoBeanSqlUtil.getInstance().searchAllTime("time", true);
                String weekDay = TimeUtils.getWeekDay();
                if (searchAllTime.size() > 0) {
                    for (AutoBean autoBean : searchAllTime) {
                        List<TimerBean> normalTimeToTimeList = AlarmUtil.normalTimeToTimeList(autoBean.getAutoTypeDetail());
                        if (normalTimeToTimeList.size() > 0) {
                            for (TimerBean timerBean : normalTimeToTimeList) {
                                if (timerBean.isOpen()) {
                                    String time = timerBean.getTime();
                                    String week = timerBean.getWeek();
                                    if (str.equals(time) && week.contains(weekDay) && autoBean.getIsEnable()) {
                                        EventBus.getDefault().post(new DoAutoBean(101, autoBean, null, 0));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        });
    }

    public static void localTime() {
        List<AutoBean> searchAllTime = AutoBeanSqlUtil.getInstance().searchAllTime("time", true);
        String weekDay = TimeUtils.getWeekDay();
        if (searchAllTime.size() > 0) {
            for (AutoBean autoBean : searchAllTime) {
                String alarmTime = TimeUtils.getAlarmTime();
                List<TimerBean> normalTimeToTimeList = normalTimeToTimeList(autoBean.getAutoTypeDetail());
                if (normalTimeToTimeList.size() > 0) {
                    for (TimerBean timerBean : normalTimeToTimeList) {
                        if (timerBean.isOpen()) {
                            String time = timerBean.getTime();
                            String week = timerBean.getWeek();
                            if (alarmTime.equals(time) && week.contains(weekDay) && autoBean.getIsEnable()) {
                                EventBus.getDefault().post(new DoAutoBean(101, autoBean, null, 0));
                            }
                        }
                    }
                }
            }
        }
    }

    public static void cancelTimer() {
        TimerTask timerTask2 = timerTask;
        if (timerTask2 != null) {
            timerTask2.cancel();
            timerTask = null;
        }
        Timer timer2 = timer;
        if (timer2 != null) {
            timer2.cancel();
            timer = null;
        }
    }

    public static List<TimerBean> normalTimeToTimeList(String str) {
        ArrayList arrayList;
        int i;
        int i2;
        new ArrayList();
        if (!TextUtils.isEmpty(str)) {
            arrayList = new ArrayGson().fromJsonList(str, TimerBean.class);
            if (arrayList == null) {
                arrayList = new ArrayList();
            }
            if (arrayList.size() == 0) {
                String[] split = str.split(":");
                String str2 = "0123456";
                int i3 = 0;
                if (split.length == 2) {
                    i3 = findNum(typeList02, split[0]);
                    i2 = findNum(typeList01, split[1]);
                    i = 0;
                } else if (split.length == 3) {
                    i3 = findNum(typeList02, split[0]);
                    i2 = findNum(typeList01, split[1]);
                    i = findNum(typeList01, split[2]);
                } else if (split.length == 4) {
                    i3 = findNum(typeList02, split[0]);
                    i2 = findNum(typeList01, split[1]);
                    int findNum = findNum(typeList01, split[2]);
                    str2 = split[3];
                    i = findNum;
                } else {
                    str2 = "";
                    i = 0;
                    i2 = 0;
                }
                arrayList.add(new TimerBean(typeList02[i3] + ":" + typeList01[i2] + ":" + typeList01[i], str2, true));
            }
        } else {
            arrayList = new ArrayList();
        }
        return arrayList == null ? new ArrayList() : arrayList;
    }

    public static int findNum(String[] strArr, String str) {
        for (int i = 0; i < strArr.length; i++) {
            if (strArr[i].endsWith(str)) {
                return i;
            }
        }
        return 0;
    }
}
